//
//  UserViewController.m
//  zhijian
//
//  Created by lcb on 2019/1/18.
//  Copyright © 2019 lcb. All rights reserved.
//

#import "UserViewController.h"
#import "Base.h"
#import "UserQuestionViewController.h"
#import "MessageViewController.h"
#import "ReplyViewController.h"
#import "CollectionsViewController.h"
#import "AdviceViewController.h"
#import "AboutViewController.h"
#import "AppDelegate.h"
#import "ProfileViewController.h"
@interface UserViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong) NSArray *dataSource;
@property(nonatomic,strong) NSArray *imgsSource;
@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   [self updateTitle: @"我的"];
    [self initData];
}
- (void)initData{
    
    NSInteger userType = [[BaseShare sharedBaseShare] getUser].userType;
    if (userType==2) {
        _dataSource = [NSArray arrayWithObjects:@"我的回复",@"我的收藏",@"意见反馈",@"关于我们",@"当前版本", nil];
        _imgsSource= [NSArray arrayWithObjects:@"adv",@"fav",@"adv",@"about",@"ver", nil];
    }else{
        _dataSource = [NSArray arrayWithObjects:@"我的咨询",@"我的收藏",@"我的消息",@"意见反馈",@"关于我们",@"当前版本", nil];
         _imgsSource= [NSArray arrayWithObjects:@"ques",@"fav",@"msg",@"adv",@"about",@"ver", nil];
    }
    
    [self.tableView reloadData];
    
}

-(UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT) style:(UITableViewStyleGrouped)];
        _tableView.backgroundColor=[UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
      
    }
    return _tableView;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return self.dataSource.count+1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 80;
}
//section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)];
    view.backgroundColor = [UIColor clearColor];
    UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sendButton.frame =CGRectMake(20, 40, SCREEN_WIDTH-40, 40);
    sendButton.layer.cornerRadius =20;
    sendButton.layer.masksToBounds = YES;
    sendButton.backgroundColor=navColor;
    [sendButton setTitle:@"退出登录" forState:UIControlStateNormal];
    [sendButton addTarget:self action:@selector(exitloginClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:sendButton];
    return view;
}
-(void)exitloginClick
{
    [BaseAlertView showAlertViewWithTitle:@"" message:@"确定退出吗？" cancleButtonTitle:@"取消" okButtonTitle:@"退出" okClickHandle:^{
        [[Api sharedApi] logout:nil callback:^(ApiResponse * _Nonnull response, NSString * _Nonnull errorMsg) {
            [BaseToast show:response.message];
            if(response.code==0)
            {
                [[BaseShare sharedBaseShare] clearToken];
                [[BaseShare sharedBaseShare] clearUser];
                [(AppDelegate*)[UIApplication sharedApplication].delegate switchLoginViewController];
            }
        }];
    } cancelClickHandle:^{
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        return 160;
    }
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TEXTCELL"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"TEXTCELL"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    if (indexPath.row==0) {
        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 160)];
        headerView.backgroundColor = [UIColor whiteColor];
        UIImageView *bgImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 160)];
        bgImg.contentMode = UIViewContentModeScaleToFill;
        bgImg.image =[UIImage imageNamed:@"usertop"];
        [headerView addSubview:bgImg];
        
        //
        UIImageView *iconImg = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-100)/2, 10, 100, 100)];
        //设置边框圆角的弧度
        [iconImg.layer setMasksToBounds:YES];
        [iconImg.layer setCornerRadius:100 / 2];
        iconImg.userInteractionEnabled = YES;
        if ([[BaseShare sharedBaseShare] getUser].headImg.length>0) {
             [iconImg imageWithUrlStr:[[BaseShare sharedBaseShare] getUser].headImg placeholder:[UIImage imageNamed:@"avater"]];
        }else{
             iconImg.image =[UIImage imageNamed:@"avater"];
        }
       
//
        [headerView addSubview:iconImg];
        //
        UILabel *lblNick = [[UILabel alloc] initWithFrame:CGRectMake(0, iconImg.frame.origin.y+110, SCREEN_WIDTH, 20)];
        lblNick.font = [UIFont systemFontOfSize:14];
        lblNick.textColor=UIColorOfHex(0xffffff);
        lblNick.text =[NSString stringWithFormat:@"%@%@%@",[[BaseShare sharedBaseShare] getUser].userName,[[BaseShare sharedBaseShare] getUser].companyName,[[BaseShare sharedBaseShare] getUser].userTitle];
        lblNick.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:lblNick];
     
        [cell addSubview:headerView];
    
    }else{
        NSString *title = [_dataSource objectAtIndex:indexPath.row-1];
        NSString *imsge = [_imgsSource objectAtIndex:indexPath.row-1];
        if ([title isEqualToString:@"当前版本"])
        {
            cell.imageView.image =[UIImage imageNamed:imsge];
            cell.textLabel.text =title;
            NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
            NSString *versionString = [NSString stringWithFormat:@"v%@",[infoDic objectForKey:@"CFBundleShortVersionString"]];
            CGSize versionSize = [versionString sizeWithFont:[UIFont systemFontOfSize:16.0f]];
            UILabel *versionLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-versionSize.width-20, 15, versionSize.width, versionSize.height)];
            versionLabel.backgroundColor = [UIColor clearColor];
            versionLabel.font = [UIFont systemFontOfSize:16.0f];
            versionLabel.textColor = UIColorOfHex(0x757575);
            versionLabel.textAlignment = NSTextAlignmentRight;
            versionLabel.text = versionString;
            versionLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
            [cell addSubview:versionLabel];
        }else{
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.imageView.image =[UIImage imageNamed:imsge];
            cell.textLabel.text =title;
        }
    }
   
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        ProfileViewController *uq =[[ProfileViewController alloc] init];
        [self.navigationController pushViewController:uq animated:YES];
    }else{
        NSString *title = [_dataSource objectAtIndex:indexPath.row-1];
        if ([title isEqualToString:@"我的咨询"]) {
            UserQuestionViewController *uq =[[UserQuestionViewController alloc] init];
            [self.navigationController pushViewController:uq animated:YES];
        }else if ([title isEqualToString:@"我的回复"]) {
            ReplyViewController *reply =[[ReplyViewController alloc] init];
            [self.navigationController pushViewController:reply animated:YES];
        }else if ([title isEqualToString:@"我的收藏"]) {
            CollectionsViewController *collection =[[CollectionsViewController alloc] init];
            [self.navigationController pushViewController:collection animated:YES];
        }else if ([title isEqualToString:@"我的消息"]) {
            MessageViewController *message =[[MessageViewController alloc] init];
            [self.navigationController pushViewController:message animated:YES];
        }else if ([title isEqualToString:@"意见反馈"]) {
            AdviceViewController *advice =[[AdviceViewController alloc] init];
            [self.navigationController pushViewController:advice animated:YES];
        }else if ([title isEqualToString:@"关于我们"]) {
            AboutViewController *about =[[AboutViewController alloc] init];
            [self.navigationController pushViewController:about animated:YES];
        }
    }
 
}
@end
